package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.repository.StrategyRepsitory;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyRepsitory strategyRepsitory;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IDestinationService destinationService;



    //容易让mongodb产生误会,后续自动生成的id不会存进去
    @Override
    public void save(Strategy strategy) {
        strategy.setId(null);
        strategyRepsitory.save(strategy);
    }

    @Override
    public void update(Strategy strategy) {
        strategyRepsitory.save(strategy);
    }

    @Override
    public void delete(String id) {
        strategyRepsitory.deleteById(id);
    }

    @Override
    public Strategy get(String id) {
        return strategyRepsitory.findById(id).orElse(null);
    }

    @Override
    public List<Strategy> list() {
        return strategyRepsitory.findAll();
    }


    //页面数据
    @Override
    public Page<Strategy> query(StrategyQuery qo) {
        //创建查询条件
        Query query = new Query();
        //添加查询目的地条件
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        //添加查询主题条件
        if (StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        //分页
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");
        return DBHelper.query(query, Strategy.class, template, pageable);
    }

    //新增或修改
    @Override
    public void saveOrUpdate(Strategy strategy) {
        //设置保存时缺少的字段数据
        //目的地id
        StrategyCatalog strategyCatalog = strategyCatalogService.get(strategy.getCatalogId());
        strategy.setDestId(strategyCatalog.getDestId());
        //攻略主题名字
        StrategyTheme strategyTheme = strategyThemeService.get(strategy.getThemeId());
        strategy.setThemeName(strategyTheme.getName());
        //分类名称
        strategy.setCatalogName(strategyCatalog.getName());
        //目的地名称
        strategy.setDestName(strategyCatalog.getDestName());
        //是否国内
        List<Destination> toasts = destinationService.queryToasts(strategyCatalog.getDestId());
        if (toasts != null && toasts.size() > 0) {
            Destination destination = toasts.get(0);
            if ("中国".equals(destination)){
                //设置为国内
                strategy.setIsabroad(strategy.ABROAD_NO);
            }else {
                //设置为国外
                strategy.setIsabroad(strategy.ABROAD_YES);
            }
        }
        if (!StringUtils.hasLength(strategy.getId())) {
            //创建时间
            strategy.setCreateTime(new Date());
            this.save(strategy);
        } else {
            //修改时把原来的数据设置回去
            Strategy temp = this.get(strategy.getCatalogId());
            strategy.setCreateTime(temp.getCreateTime());
            strategy.setFavornum(temp.getFavornum());
            strategy.setReplynum(temp.getReplynum());
            strategy.setSharenum(temp.getSharenum());
            strategy.setViewnum(temp.getViewnum());
            strategy.setThumbsupnum(temp.getThumbsupnum());
            this.update(strategy);
        }
    }

    //通过分类id查询攻略明细
    @Override
    public List<Strategy> QueryByCatalogId(String catalogId) {
        List<Strategy> strategies = strategyRepsitory.findByCatalogId(catalogId);
        return strategies;
    }


    //查询点击量最高3篇
    @Override
    public List<Strategy> QueryTop3ByDestId(String destId) {
        Pageable pageable = PageRequest.of(0,3,Sort.Direction.DESC,"viewnum");
        List<Strategy> strategies = strategyRepsitory.findByDestId(destId, pageable);
        return strategies;
    }

    //更新vo数据到数据库
    @Override
    public void updateStatisVo(StrategyStatisVO vo) {
        //创建查询条件
        Query query = new Query();
        //存入条件
        query.addCriteria(Criteria.where("_id").is(vo.getStrategyId()));
        //创建更新对象
        Update update = new Update();
        //修改对应的数据
        update.set("favornum",vo.getFavornum());
        update.set("replynum",vo.getReplynum());
        update.set("sharenum",vo.getSharenum());
        update.set("thumbsupnum",vo.getThumbsupnum());
        update.set("viewnum",vo.getViewnum());
        //执行修改
        template.updateMulti(query,update,Strategy.class);
    }

    //查询目的地下攻略
    @Override
    public List<Strategy> queryByDestId(String destId) {
        return strategyRepsitory.findByDestId(destId,null);
    }
}
